/*
 * bsp_rmu.c
 *
 *  Created on: 2023年7月22日
 *      Author: HXF
 */

#include "bsp_rmu.h"
#include "em_rmu.h"
#include "stdio.h"
#include <string.h>
static uint32_t rst_cause;

uint32_t ds_rmu_init(void)
{
  uint32_t rst = RMU_ResetCauseGet();

  if(rst & EMU_RSTCAUSE_POR){
      rst_cause = DS_RSTCAUSE_POR;
  }else if(rst & EMU_RSTCAUSE_PIN){
      rst_cause = DS_RSTCAUSE_PIN;
  }else if(rst & EMU_RSTCAUSE_EM4){
      rst_cause = DS_RSTCAUSE_EM4;
  }else if(rst & EMU_RSTCAUSE_WDOG0){
      rst_cause = DS_RSTCAUSE_WDOG0;
  }else if(rst & EMU_RSTCAUSE_WDOG1){
      rst_cause = DS_RSTCAUSE_WDOG1;
  }else if(rst & EMU_RSTCAUSE_LOCKUP){
      rst_cause = DS_RSTCAUSE_LOCKUP;
  }else if(rst & EMU_RSTCAUSE_SYSREQ){
      rst_cause = DS_RSTCAUSE_SYSREQ;
  }else if(rst & EMU_RSTCAUSE_DVDDBOD){
      rst_cause = DS_RSTCAUSE_DVDDBOD;
  }else if(rst & EMU_RSTCAUSE_DVDDLEBOD){
      rst_cause = DS_RSTCAUSE_DVDDLEBOD;
  }else if(rst & EMU_RSTCAUSE_DECBOD){
      rst_cause = DS_RSTCAUSE_DECBOD;
  }else if(rst & EMU_RSTCAUSE_AVDDBOD){
      rst_cause = DS_RSTCAUSE_AVDDBOD;
  }else if(rst & EMU_RSTCAUSE_IOVDD0BOD){
      rst_cause = DS_RSTCAUSE_IOVDD0BOD;
  }else if(rst & EMU_RSTCAUSE_SETAMPER){
      rst_cause = DS_RSTCAUSE_SETAMPER;
  }else if(rst & EMU_RSTCAUSE_VREGIN){
      rst_cause = DS_RSTCAUSE_VREGIN;
  }else {
      rst_cause = rst;
  }

  RMU_ResetCauseClear();
  return rst_cause;
}

void resetToString(uint32_t rst_cause, char *reason)
{
  switch(rst_cause) {
    case DS_RSTCAUSE_POR:
      memcpy(reason, "POR", sizeof("POR"));
      break;
    case DS_RSTCAUSE_PIN:
      memcpy(reason, "PIN", sizeof("PIN"));
      break;
    case DS_RSTCAUSE_EM4:
      memcpy(reason, "EM4", sizeof("EM4"));
      break;
    case DS_RSTCAUSE_WDOG0:
      memcpy(reason, "WDOG0", sizeof("WDOG0"));
      break;
    case DS_RSTCAUSE_WDOG1:
      memcpy(reason, "WDOG1", sizeof("WDOG1"));
      break;
    case DS_RSTCAUSE_SYSREQ:
      memcpy(reason, "SYSREQ", sizeof("SYSREQ"));
      break;
    case DS_RSTCAUSE_DVDDBOD:
      memcpy(reason, "DVDDBOD", sizeof("DVDDBOD"));
      break;
    case DS_RSTCAUSE_AVDDBOD:
      memcpy(reason, "AVDDBOD", sizeof("AVDDBOD"));
      break;
    default:
      sprintf(reason, "0x%08lx", rst_cause);
      break;
  }
}

//获取本次重启的重启原因
DS_RSTCAUSE_E getRestReason(void)
{
  return rst_cause;
}
